<template>
  <div>
    <van-nav-bar
      class="page-nav-bar"
      title="个人中心"
      left-arrow
      @click-left="$router.back()"
    />
    <input type="file" hidden ref="fileRef" @change="onFilechange" />
    <van-cell title="头像" is-link @click="$refs.fileRef.click()">
      <van-image class="avator" round fit="cover" :src="user.photo" />
    </van-cell>
    <van-cell
      title="昵称"
      :value="user.name"
      is-link
      @click="isUpdateNameShow = true"
    ></van-cell>
    <van-cell
      title="性别"
      :value="user.gender === 0 ? '男' : '女'"
      is-link
      @click="isUpdateGenderShow = true"
    ></van-cell>
    <van-cell
      title="生日"
      :value="user.birthday"
      is-link
      @click="isUpdateBirthdayShow = true"
    ></van-cell>
    <!-- 姓名 -->
    <van-popup
      v-model="isUpdateNameShow"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <update-name
        @close="isUpdateNameShow = false"
        v-model="user.name"
        v-if="isUpdateNameShow"
      ></update-name>
    </van-popup>
    <!-- 性别 -->
    <van-popup v-model="isUpdateGenderShow" position="bottom">
      <update-gender
        @close="isUpdateGenderShow = false"
        v-model="user.gender"
        v-if="isUpdateGenderShow"
      ></update-gender>
    </van-popup>
    <!-- 生日 -->
    <van-popup v-model="isUpdateBirthdayShow" position="bottom">
      <update-birthday
        @close="isUpdateBirthdayShow = false"
        v-model="user.birthday"
        v-if="isUpdateBirthdayShow"
      ></update-birthday>
    </van-popup>
    <!-- 头像 -->
    <van-popup
      v-model="isUpdatePhotoShow"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <update-photo
        @update-photo="user.photo = $event"
        @close="isUpdatePhotoShow = false"
        :img="imgPath"
        v-if="isUpdatePhotoShow"
      ></update-photo>
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import updateName from './componennts/update-name.vue'
import UpdateGender from './componennts/update-gender.vue'
import UpdateBirthday from './componennts/update-birthday.vue'
import UpdatePhoto from './componennts/updat-photo.vue'

export default {
  name: 'userProfile',
  props: {},
  components: {
    updateName,
    UpdateGender,
    UpdateBirthday,
    UpdatePhoto
  },
  data () {
    return {
      isUpdatePhotoShow: false,
      user: {},
      isUpdateNameShow: false,
      isUpdateGenderShow: false,
      isUpdateBirthdayShow: false,
      imgPath: ''
    }
  },
  async created () {
    const { data: { data } } = await getUserProfile()
    this.user = data
  },
  methods: {
    onFilechange () {
      const imgPath = URL.createObjectURL(this.$refs.fileRef.files[0])
      this.imgPath = imgPath
      this.isUpdatePhotoShow = true
      this.$refs.fileRef.value = ''
    }
  }
}
</script>

<style lang="less" scoped>
.avator {
  width: 60px;
  height: 60px;
}
.van-popup {
  background-color: #f5f7f9;
}
</style>
